import { BASE_URL } from '@/config';
import { request } from '@/utils/request';

/**
 * 消息中心相关接口
 */

// ==================== 通知消息 ====================

// 获取消息通知
export function getNotifications() {
  return request({
    url: `${BASE_URL}/user/messages/notifications`,
    method: 'GET'
  });
}

// ==================== 消息会话 ====================

// 获取消息会话列表
export function getMessageSessions() {
  return request({
    url: `${BASE_URL}/user/messages/sessions`,
    method: 'GET'
  });
}

// 获取会话聊天记录
export interface GetHistoryParams {
  page?: number;
  size?: number;
}

export function getSessionHistory(sessionId: number, params?: GetHistoryParams) {
  return request({
    url: `${BASE_URL}/user/messages/sessions/${sessionId}/history`,
    method: 'GET',
    params: {
      page: params?.page || 1,
      size: params?.size || 20
    }
  });
}

// 发送消息
export interface SendMessageRequest {
  content: string;
  messageType: number;
  attachments?: {
    url: string;
    type: string;
    size: number;
  };
  relatedOrderId?: string;
}

export function sendMessage(sessionId: number, data: SendMessageRequest) {
  return request({
    url: `${BASE_URL}/user/messages/sessions/${sessionId}/messages`,
    method: 'POST',
    data
  });
}

// 标记消息为已读
export interface MarkReadRequest {
  readAll?: boolean;
  chatIds?: number[];
}

export function markMessagesAsRead(sessionId: number, data: MarkReadRequest) {
  return request({
    url: `${BASE_URL}/user/messages/sessions/${sessionId}/mark-read`,
    method: 'POST',
    data
  });
}

// 清空聊天记录
export function deleteSession(sessionId: number) {
  return request({
    url: `${BASE_URL}/user/messages/sessions/${sessionId}`,
    method: 'DELETE'
  });
}

// 获取未读消息统计
export function getUnreadCount() {
  return request({
    url: `${BASE_URL}/user/messages/unread-count`,
    method: 'GET'
  });
}